package com.gj.sendall;

import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;

import javax.websocket.Session;

public class SendMsgRun implements Runnable {

	private LinkedBlockingQueue<String> queue;// 阻塞队列 
	Map<String, Session> conns;
	private boolean stop = true;
	
	public SendMsgRun(LinkedBlockingQueue<String> queue,Map<String, Session> conns) {
		this.queue = queue; 
		this.conns = conns;
	}
	
	@Override
	public void run(){ 
		while(stop) {
			toDo();
		} 
	} 
	
	private void toDo(){
		try { 
			String data = queue.take();//.poll(2, TimeUnit.SECONDS);// 有数据时直接从队列的队首取走，无数据时阻塞，在2s内有数据，取走，超过2s还没数据，返回失败
			for(Session session : conns.values()){
				System.out.println(data);
				System.out.println(session.toString());
				//session.getAsyncRemote().sendText(data);
			}
		} catch (Exception e) {
			e.printStackTrace(); 
		}
	}  
}